﻿using AngleSharp.Html.Parser;
using SpiderPlatform.Dao;
using SpiderPlatform.Entity;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
namespace SpiderPlatform
{
    public class TagService
    {
        HtmlParser htmlParser = new HtmlParser();
        ItemDao itemDao = new ItemDao();
        public void doWork() {
            List<ProductTag> ProductList = new List<ProductTag>();  // new List<ProductTag> { new ProductTag { title= "江中猴姑" } };
            List<Task> taskList = new List<Task>();
            List<Task> taskList2 = new List<Task>();
            List<ProductTagInfo> infos = new List<ProductTagInfo>();
            int rec_id = 75373;
            do
            { 
                ProductList = itemDao.QueryChannelItemById(rec_id);
                if (ProductList.Count == 0)
                {
                    Logger.Info("已没有记录。");
                    break;
                }
               
                taskList.Clear();
                taskList2.Clear();
                foreach (var item in ProductList)
                {
                    taskList.Add(Task.Factory.StartNew(async () => {
                        var t2 = GetTagsAsync(item.title);
                        taskList2.Add(t2);
                        item.tagList = await t2;
                        foreach (var t in item.tagList)
                        {
                            t.channel_id = item.channel_id;
                            t.channel_sku = item.channel_sku;
                        }
                    }));                }             
                Task.WaitAll(taskList.ToArray());
                Logger.Info("当前为下载开始,最小id为" + ProductList.Min(p => p.rec_id));
                Logger.Info("当前最大id为" + rec_id);
                Task.WaitAll(taskList2.ToArray());
                infos.Clear();
                foreach (var item in ProductList)
                {
                    infos.AddRange(item.tagList);
                }
                if (infos.Count>0)
                {
                    itemDao.CreateProductTag(infos);
                }
                rec_id = ProductList.Max(p => p.rec_id);
           
            } while (true);          
            Logger.Info("全部下载完成");
        }

        async Task<List<ProductTagInfo>> GetTagsAsync(string title)
        {
            List<ProductTagInfo> list = new List<ProductTagInfo>();
            try
            {
                HttpClient client = new HttpClient();
                Logger.Info($"process {title}");
                var result = await client.GetStringAsync($"https://so.m.jd.com/ware/search.action?keyword={title}&searchFrom=search&sf=11&as=1");
                var dom = htmlParser.ParseDocument(result);
                var items = dom.QuerySelectorAll("#itemList div.search_prolist_item");
                if (items != null)
                {
                    int index = 1;
                    foreach (var item in items)
                    {
                        list.Add(new ProductTagInfo
                        {
                            tag = item.GetAttribute("skuid"),
                            order = index++,
                            title = item.QuerySelector(".search_prolist_title").InnerHtml.Replace("\n", "").Trim().Replace("'","’")
                        }); ;
                    }
                }
            }
            catch (Exception)
            {
                return list;
            }
            return list;
        }
    }
}
